// pages/createorder/createorder.js
var baserul = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payok: "none",
    payfail: "none"
  },

  showOK: function() {
    this.setData({
      payok: ""
    });
  },
  showfail: function() {
    this.setData({
      payfail: ""
    });
  },
  buy: function() {
    this.showOK();
  },
  tomyorder: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  toindex: function() {
    wx.navigateTo({
      url: '../index/index'
    })
  },
  tomyorder: function() {
    wx.navigateTo({
      url: '../myorder/myorder'
    })
  },
  createOrder: function() {
    var that = this;
    wx.request({
      url: baseurl + "",
      data: {
        ticketId: 1,
        count: 1,
        openid: that.userInfo.openid,
      },
      success: function(d) {
        if (d.code == 10000) {
          wx.requestPayment({
            'timeStamp': d.data.timeStamp,
            'nonceStr': d.data.nonceStr,
            'package': d.data.package,
            'signType': 'MD5',
            'paySign': d.data.paySign,
            'success': function(res) {
              if (res.requestPayment == "ok") {
                this.showOK();
              }
            },
            'fail': function(res) {
              this.showfail();
            },
            'completev': function(res) {
              if (res.requestPayment == "cancel") {
                this.showfail();
              }
            }
          })
        }
      }
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var userInfo = wx.getStorageSync('userinfo');
    if (userInfo == "" || userInfo.openid == undefined) {
      wx.login({
        success: function(res) {
          if (res.code) {
            console.log(res.code);
            wx.getUserInfo({
              withCredentials: false,
              success: function(userInfo) {
                console.log(userInfo);
                //  userInfo.openid = da.data.openid;
                //更新数据
                that.setData({
                  userInfo: userInfo.userInfo
                })
                // wx.setStorageSync('userinfo', userInfo);


              },
              fail: function(e) {
                console.log(e);
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {
      this.setData({
        userInfo: userInfo.userInfo
      })
      options.openid = userInfo.openid;
      this.initroom(options);
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    console.log("hide");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    console.log("unload");
    return false;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }

})